//
//  GZSQLTool.m
//  GZSQLModelTool_Example
//
//  Created by Guo Rui on 2018/1/25.
//  Copyright © 2018年 GR. All rights reserved.
//

#import "GZSQLTool.h"
#import <FMDB/FMDB.h>

#define kCachePath @"/Users/apple/Desktop/"

static FMDatabase *db = nil;

@implementation GZSQLTool

+ (BOOL)executeUpdate:(NSString *)sql uid:(NSString *)uid
{
    // 1. 打开数据库
    if ([self openDB:uid] == NO) {
        NSLog(@"打开数据库失败");
        return NO;
    }
    
    // 2. 执行操作
    BOOL ok = [db executeUpdate:sql];
    
    if (!ok) {
        NSLog(@"执行update语句失败");
    }
    
    // 3. 关闭数据库
    [db close];
    
    return ok;
}

+ (BOOL)executeUpdate:(NSString *)sql withArgumentsInArray:(NSArray *)arguments uid:(NSString *)uid
{
    // 1. 打开数据库
    if (![self openDB:uid]) {
        NSLog(@"打开数据库失败");
        return NO;
    }
    
    // 2. 执行操作
    BOOL ok = [db executeUpdate:sql withArgumentsInArray:arguments];
    
    if (!ok) {
        NSLog(@"执行update语句失败");
    }
    
    // 3. 关闭数据库
    [db close];
    
    return ok;
}

+ (NSArray <NSDictionary *>*)executeQuery:(NSString *)sql uid:(NSString *)uid
{
    // 1. 打开数据库
    if (![self openDB:uid]) {
        NSLog(@"打开数据库失败");
        return nil;
    }
    
    // 2. 执行操作
    FMResultSet *set = [db executeQuery:sql];
    
    NSMutableArray *recoredDicts = [NSMutableArray array];
    
    NSArray *columnNames = set.columnNameToIndexMap.allKeys;
    while (set.next) {
        NSMutableDictionary *recordDict = [NSMutableDictionary dictionary];
        for (NSString *columnName in columnNames) {
            id value = [set objectForColumn:columnName];
            if (value) {
                [recordDict setObject:value forKey:columnName];
            }
        }
        [recoredDicts addObject:recordDict];
    }
    
    // 3. 关闭数据库
    [db close];
    
    return recoredDicts.copy;
}

+ (NSArray<NSDictionary *> *)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray *)arguments uid:(NSString *)uid
{
    // 1. 打开数据库
    if (![self openDB:uid]) {
        NSLog(@"打开数据库失败");
        return nil;
    }
    
    // 2. 执行操作
    FMResultSet *set = [db executeQuery:sql withArgumentsInArray:arguments];
    
    NSMutableArray *recoredDicts = [NSMutableArray array];
    
    NSArray *columnNames = set.columnNameToIndexMap.allKeys;
    while (set.next) {
        NSMutableDictionary *recordDict = [NSMutableDictionary dictionary];
        for (NSString *columnName in columnNames) {
            id value = [set objectForColumn:columnName];
            if (value) {
                [recordDict setObject:value forKey:columnName];
            }
        }
        [recoredDicts addObject:recordDict];
    }
    
    // 3. 关闭数据库
    [db close];
    
    return recoredDicts.copy;
}

+ (BOOL)openDB:(NSString *)uid
{
    NSString *path = @"common.sqlite";
    if (uid.length > 0) {
        path = [uid stringByAppendingString:@".sqlite"];
    }
    NSString *dbPath = [kCachePath stringByAppendingString:path];
    db = [FMDatabase databaseWithPath:dbPath];
    
    return [db open];
}

+ (BOOL)executeStatements:(NSString *)sqls uid:(NSString *)uid
{
    // 1. 打开数据库
    if (![self openDB:uid]) {
        NSLog(@"打开数据库失败");
        return NO;
    }
    
    [db beginTransaction];
    BOOL ok = [db executeStatements:sqls];
    [db commit];
    
    return ok;
}

@end
